//商家订单列表
var basePage = require('../../common/BasePage');
var util = require('../../utils/util');
Page({
    behaviors: [basePage],
    data: {
		orderId:0,
        orderList:[],
		splitList:[],
		intro:""
		
    },

    onLoad: function (options) {
		this.setData({
			orderId:options.orderId
		})
		this.getOrder()
      // const eventChannel = this.getOpenerEventChannel()
      // let that = this;
      // eventChannel.on('orderDetail', function (data) {
      //   console.log("拆单订单信息", data);
    	  
      //   that.setData({
      //     orderInfo:data
      //   })
      // })
    },
    onShow: function () {
        
    },
	getOrder(e) {
	    this.request({
	        url: this.data.host + '/home/order/single',
	        data: {
	            order_id: this.data.orderId,
	        },
	        method: "GET",
	    }, res => {
	        console.log('订单信息' , res);
			var list=res.data
			list.map(val=>{
				val.split_num=0
				val.surplus_num=val.real_product_number//剩余数量
			})
			this.setData({
				orderList:res.data
			})
	    })
	},
	
	//点击删除按钮原订单
	closeOrder(e){
		var index=e.currentTarget.dataset.index//店家的角标
		var list1=this.data.orderList//原订单
		var list2=this.data.splitList//拆单订单
		var info=list1[index]
		console.log("拆单",info)
		list1.splice(index,1)
		if(list2.length<=0){
			info.split_num=info.real_product_number
			list2.push(info)
		}else{
			var state=-1//如果拆单中有该商品state为角标，没有-1
			for(var i=0;i<list2.length;i++){
				if(list2[i].pid==info.pid){
					state=i
				}
			}
			if(state==-1){
				info.split_num=info.real_product_number
				list2.push(info)
			}else{
				list2[state].split_num=info.real_product_number
			}
		}
		console.log("原列表",list1)
		console.log("拆单列表",list2)
		this.setData({
			orderList:list1,
			splitList:list2
		})
		
	},
	//点击修改数量原订单
	onChange(e){
		this.changeNum(e)
	},
	onBlur(e){
		if(e.detail.value==0){
			return;
		}
		this.changeNum(e)
		
	},
	onMinus(e){
		this.changeNum(e)
	},
	changeNum(e){
		console.log("步进器",e)
		var index=e.currentTarget.dataset.index//商品的角标
		var list1=this.data.orderList//原订单
		var list2=this.data.splitList//拆单订单
		var info=list1[index]
		var num=0;
		if(e.type=="blur"){//输入框
			num=e.detail.value
		}else if(e.type=="plus"){
			num=info.split_num+1
		}else if(e.type=="minus"){
			num=info.split_num-1
		}
		
		
		console.log("订单信息list1",list1)
		list1[index].surplus_num=list1[index].real_product_number-num
		
		console.log("订单信息list12",list1)
		if(list2.length<=0){
			info.split_num=num
			list2.push(info)
		}else{
			var state=-1//如果拆单中有该商品state为角标，没有-1
			for(var i=0;i<list2.length;i++){
				if(list2[i].pid==info.pid){
					state=i
				}
			}
			if(state==-1){
				info.split_num=num
				list2.push(info)
			}else if(num>0){
				list2[state].split_num=num
			}else if(num<=0){
				list2.splice(state,1)
			}
		}
		
		console.log("原列表",list1)
		console.log("拆单列表",list2);
		this.setData({
			orderList:list1,
			splitList:list2
		})
		if(num==info.real_product_number){//如果数量=商品购买数，移除商品
			list1.splice(index,1)
			this.setData({
				orderList:list1
			})
		}
		
		
	},
	//拆单列表点击删除
	closeOrder2(e){
		var index=e.currentTarget.dataset.index//店家的角标
		var list1=this.data.orderList//原订单
		var list2=this.data.splitList//拆单订单
		var info=list2[index]
		info.split_num=0
		list2.splice(index,1)
		if(list1.length<=0){
			info.surplus_num=info.real_product_number
			
			list1.push(info)
		}else{
			var state=-1//如果拆单中有该商品state为角标，没有-1
			for(var i=0;i<list1.length;i++){
				if(list1[i].pid==info.pid){
					state=i
				}
			}
			if(state==-1){
				info.surplus_num=info.real_product_number
				list1.push(info)
			}else{
				list1[state].surplus_num=info.real_product_number
			}
		}
		
		this.setData({
			orderList:list1,
			splitList:list2,
			[`orderList${index}.split_num`]:list1[index].split_num*1
		})
	},
	onChangeIntro(e){
		console.log("备注",e)
		this.setData({
			intro:e.detail.value
		})
	},
	//提交拆单
	submit_click(){
		var list1=this.data.orderList//原订单
		var list2=this.data.splitList//拆单订单
		console.log("原来",list1)
		console.log("拆单",list2)
		if(list1.length<=0){
			console.log("原来111111111")
			wx.showToast({
			  title: '不能把所有的订单明细都拆出去！',
			  icon: 'none',
			})
			return;
		}
		if(list2.length<=0){
			wx.showToast({
			  title: '请至少拆分一笔明细作为新的订单！',
			  icon: 'none',
			})
			return;
		}
		
		var submit=[]
		list2.map((val)=>{
			var obj={
				pid:val.pid,
				number:val.split_num
			}
			submit.push(obj)
        })
        this.data.intro= util.replaceStr(this.data.intro)
		this.request({
		    url: this.data.host + '/home/order/single',
		    data: {
				 order_id: this.data.orderId,
				 single:submit,
				 intro:this.data.intro
		    },
		    method: "POST",
		}, res => {
			wx.showModal({
			  showCancel: false,
			  title: '恭喜',
			  content: res.msg,
			  success: (result) => {
			    wx.navigateBack({
			      delta: 1,
			    })
			  },
			})
		})
		
	}
})